﻿// ===============================================================================
// XCI.NET 西安交通信息WinForm开发框架 2018 版权所有
// ===============================================================================
using DevExpress.Data;
using DevExpress.XtraGrid;
using DevExpress.XtraGrid.Columns;
using DevExpress.XtraGrid.Internal;
using DevExpress.XtraGrid.Registrator;
using DevExpress.XtraGrid.Views.Base;
using DevExpress.XtraGrid.Views.Base.Handler;
using DevExpress.XtraGrid.Views.Base.ViewInfo;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using XCI.Helper;

namespace XCI.Windows.Controls
{
    [System.ComponentModel.DesignerCategory("Code")]
    [ToolboxItem(true)]
    public class GridControlx : DevExpress.XtraGrid.GridControl
    {
        private string _id = StringHelper.Guid();

        /// <summary>
        /// 表格Id
        /// </summary>
        [Browsable(false)]
        public virtual string Id
        {
            get => _id;
            set => _id = value;
        }

        /// <summary>
        /// 分页控件
        /// </summary>
        [Browsable(true), Category("XCI"), Description("分页控件")]
        public GridPager Pager { get; set; }

        protected override BaseView CreateDefaultView()
        {
            return CreateView("GridViewx");
        }

        protected override void RegisterAvailableViewsCore(InfoCollection collection)
        {
            base.RegisterAvailableViewsCore(collection);
            collection.Add(new GridViewxInfoRegistrator());
        }
    }

    public class GridViewxInfoRegistrator : GridInfoRegistrator
    {
        public override string ViewName => "GridViewx";

        public override BaseView CreateView(GridControl grid)
        {
            return new GridViewx(grid);
        }

        public override BaseViewInfo CreateViewInfo(BaseView view)
        {
            return new GridViewxInfo(view as GridViewx);
        }

        public override BaseViewHandler CreateHandler(BaseView view)
        {
            return new GridViewxHandler(view as GridViewx);
        }

        public override BaseViewPainter CreatePainter(BaseView view)
        {
            return new GridViewxPainter(view as GridViewx);
        }
    }

    public class GridViewx : DevExpress.XtraGrid.Views.Grid.GridView
    {
        public GridViewx()
        {
        }

        public GridViewx(GridControl grid) : base(grid)
        {
        }

        protected override string ViewName => "GridViewx";

        public event EventHandler<CollectionChangeEventArgs> ColumnSortInfoChanged;

        protected override void OnColumnSortInfoCollectionChanged(CollectionChangeEventArgs e)
        {
            base.OnColumnSortInfoCollectionChanged(e);
            ColumnSortInfoChanged?.Invoke(this, e);
        }

        protected override List<IDataColumnInfo> GetFindToColumnsCollection()
        {
            return base.GetFindToColumnsCollection().Concat(
                Columns.Where(c => !c.Visible).Select(CreateIDataColumnInfoForFilter)).ToList();
        }

        private new static IDataColumnInfo CreateIDataColumnInfoForFilter(GridColumn column)
        {
            return new GridColumnIDataColumnInfoWrapper(column,
                GridColumnIDataColumnInfoWrapperEnum.Filter);
        }
    }

    public class GridViewxInfo : DevExpress.XtraGrid.Views.Grid.ViewInfo.GridViewInfo
    {
        public GridViewxInfo(DevExpress.XtraGrid.Views.Grid.GridView view) : base(view)
        {
        }
    }

    public class GridViewxHandler : DevExpress.XtraGrid.Views.Grid.Handler.GridHandler
    {
        public GridViewxHandler(DevExpress.XtraGrid.Views.Grid.GridView view) : base(view)
        {
        }
    }

    public class GridViewxPainter : DevExpress.XtraGrid.Views.Grid.Drawing.GridPainter
    {
        public GridViewxPainter(DevExpress.XtraGrid.Views.Grid.GridView view) : base(view)
        {
        }
    }
}